---
title: "Maps for Democracy's Backsliding in the International Environment"
author: "Susan Hyde and Alex Stephenson"
date: "7/23/2020"
output:
  html_document:
    df_print: paged
    code_folding: hide
  pdf_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Packages 

To replicate these maps, the following packages must be installed. 

```{r, eval = F}
install.packages(c("cshapes", "countrycode", 
                   "data.table", "here",
                   "rnaturalearth", "rnaturalearthdata",
                   "rcartocolor", "sf", "tidyverse", 
                   "rgeos"))

```

```{r, message = F, warning = F}
library(cshapes)
library(countrycode)
library(data.table)
library(here)
library(rnaturalearth)
library(rnaturalearthdata)
library(rcartocolor)
library(sf)
library(tidyverse)

```

## V-Dem Data 

V-Dem data for historical countries will be matched with COWCode. For example, Yugoslavia in 1976 will be matched with V-Dem's Serbia score for 1976 because Serbia has the same COWCode as Yugoslavia in that year. 

```{r, warning = F, message = F}
r_vdem <- fread(here("data/V-Dem-CY-Core-v10.csv"))

vdem <- r_vdem %>% 
    select(contains("country"), COWcode, year, v2x_polyarchy)%>% 
  mutate(v2x_polyarchy_d = case_when(
    v2x_polyarchy <= .25 ~ ".25",
    v2x_polyarchy > .25 & v2x_polyarchy <= .50~".5",
    v2x_polyarchy > .5 & v2x_polyarchy <= .75~".75",
    v2x_polyarchy > .75~"1"
  ))

# Dates for 4 panel 1976, 1989, 1999, 2019
vdem76 <- vdem %>% 
    filter(year == 1976)
vdem89 <- vdem %>% 
    filter(year == 1989)
vdem95 <- vdem %>% 
    filter(year == 1995)
vdem99 <- vdem %>% 
    filter(year == 1999)
vdem09 <- vdem %>% 
    filter(year == 2009)
vdem15 <- vdem %>% 
    filter(year == 2015)
vdem19 <- vdem %>% 
    filter(year == 2019)
```

## Make map function 

```{r}
makeMap <- function(df, year = "Default", name = name, eps = T){
    folder <- here("data/plots/")
    theme_opts <- list(theme(panel.grid.minor = element_blank(),
                        panel.grid.major = element_blank(),
                        panel.background = element_blank(),
                        panel.border = element_blank(),
                        axis.line = element_blank(),
                        axis.text.x = element_blank(),
                        axis.text.y = element_blank(),
                        axis.ticks = element_blank(),
                        axis.title.x = element_blank(),
                        axis.title.y = element_blank(),
                        plot.title = element_text(size=22)))
    g <- ggplot(df)+ 
    # Function assumes that there is a variable called 
    # v2x_polyarchy_d, a discrete scale variable
        geom_sf(aes(fill = v2x_polyarchy_d), size = .1)+
    # For now, this just defaults to the viridis palette
        scale_fill_carto_d(name = "Electoral Democracy Level", type = "qualitative", palette="Mint")+
        coord_sf()+
        theme_opts+
        theme(legend.position = "bottom")+
        ggtitle(year)
    if(eps == T){
        ggsave(filename = paste0(folder, name,".eps"),
               plot = g,
               device = "eps",
               width = 20,
               height = 20, 
               units = "cm") 
    }else{
        ggsave(filename = paste0(folder, name,".eps"),
               plot = g,
               device = "pdf") 
    }
    return(g)
}
```


## Read in Map Data 

```{r, message = F, warning = F}
# Use cshapes package
map_76 <- cshp(date = as.Date("1976-1-1"))%>%
    st_as_sf()
map_89 <- cshp(date = as.Date("1989-1-1"))%>%
    st_as_sf()
map_99 <- cshp(date = as.Date("1999-1-1"))%>%
    st_as_sf()

# cshapes only goes up until 2016. Using rnaturalearth's worldmap for 2019
map_19 <- ne_countries(scale = "medium", type = "countries", returnclass = "sf")%>%
  filter(!region_wb == "Antarctica",
         !admin == "Greenland")%>%
    # Add COW Code and manually assign Serbia's
    mutate(sovereignt = str_replace_all(sovereignt, "Republic of Serbia", "Serbia"),
        COWCODE = countrycode(sovereignt, "country.name", "cown"),
        COWCODE = ifelse(sovereignt == "Serbia", 345, COWCODE))
```

## 4 Panel 

```{r}
# Match country identifiers between V-Dem and cshapes
# Use COWcode from V-Dem to match with cshapes polygons 
# Remove microstates that are not listed by COW
map_1976 <- left_join(map_76, vdem76, by=c("COWCODE"="COWcode"))%>%
  filter(!is.na(v2x_polyarchy_d))
map_1989 <- left_join(map_89, vdem89, by=c("COWCODE"="COWcode"))%>%
  filter(!is.na(v2x_polyarchy_d))
map_1999 <- left_join(map_99, vdem99, by=c("COWCODE"="COWcode"))%>%
  filter(!is.na(v2x_polyarchy_d))
map_2019 <- left_join(map_19, vdem19, by=c("COWCODE"="COWcode"))%>%
  filter(!is.na(v2x_polyarchy_d))
```

## Make Maps 

```{r, message = F}
makeMap(map_1976, "1976", name = "plot76")
makeMap(map_1989, "1989", name = "plot89")
makeMap(map_1999, "1999", name = "plot99")
makeMap(map_2019, "2019",  name = "plot19")
```